<?php
/**
 * Created by PhpStorm.
 * User: 帅帅吴
 * Date: 2018/11/12
 * Time: 14:53
 */

namespace app\admin\model;


class ActivityBoxLog extends DataBase
{
    protected $name = 'activity_box_log';
    protected $autoWriteTimestamp = 'true';

    public function getTypeAttr($value)
    {
        if($value==1) return '普通礼物';
        if($value==2) return '座驾';
    }
    public function getAddTimeAttr($value)
    {
        return date('Y-m-d H:i:s', $value);
    }

    public function getDataList()
    {
        $page_config = array(
            'page' =>input('get.offset',1),
            'list_rows' =>input('get.limit',10),
        );
        $order = input('sort', 'a.id') . ' ' . input('order', 'desc');
        $where = [];
        if(!empty(input('key')) && !empty(input('value'))){
            $key = input('key');
            $value = input('value');
            $where[] = ['b.'.$key,'=',$value];
        }
        if(!empty(input('start_time')) && !empty(input('end_time'))){
            $start_time = strtotime(input('start_time'));
            $end_time = strtotime(input('end_time'));
            $where[] = ['add_time','between',[$start_time,$end_time]];
        }
        $data = $this
            ->alias('a')
            ->field('a.id,b.u_id,b.nickname,c.type,a.prize_id,a.number,a.add_time')
            ->join('users b','a.user_id = b.id','left')
            ->join('activity_box_prize c','a.prize_id = c.type_id','left')
            ->where($where)
           ->order($order)
            ->paginate($page_config);
        foreach($data as $key => $value){
            if($value['type'] == '普通礼物'){
                $data[$key]['gift_name'] = Gift::where('id='.$value['prize_id'])->value('gift_name');
            }
            if($value['type'] == '座驾'){
                $data[$key]['gift_name'] = Vehicle::where('vehicle_id='.$value['prize_id'])->value('vehicle_name');
            }
        }
        return $data;

    }

}